package com.zxjbyte.yiyi.framework.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zxjbyte.yiyi.framework.mybatis.handler.YiyiMetaObjectHandler;
import com.zxjbyte.yiyi.framework.mybatis.injector.YiyiSqlInjector;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @Author zhangxingjia
 * @Date 2023/3/2 17:52
 * @Version: 1.0
 */
@SuppressWarnings("SpringComponentScan")
@AutoConfiguration
@MapperScan(value = "${info.app.base-package}", annotationClass = Mapper.class)
@EnableConfigurationProperties(MybatisPlusProperties.class)
public class YiyiMybatisAutoConfiguration {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); // 防全表更新与删除插件
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件
        return mybatisPlusInterceptor;
    }

    @Bean
    public MetaObjectHandler defaultMetaObjectHandler(){
        return new YiyiMetaObjectHandler(); // 自动填充参数类
    }

    /**
     * 自定义sql注入器
     */
    @Bean
    @ConditionalOnMissingBean(ISqlInjector.class)
    public ISqlInjector sqlInjector() {
        return new YiyiSqlInjector();
    }

}
